package com.example.agvctrl;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class JWebSocketClientService extends Service {
    URI uri ;
    public JWebSocketClient client;
    public JWebSocketClientBinder mbinder = new JWebSocketClientBinder();
    class JWebSocketClientBinder extends Binder{
        public JWebSocketClientService getService(){
            return JWebSocketClientService.this;
        }
    }
    public JWebSocketClientService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return mbinder;
        //throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        closeClintConnect();
        return super.onUnbind(intent);
    }
    private void closeClintConnect() {
        try {
            if (null != client) {
                client.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            client = null;
        }
    }
    public void connect(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    client.connect();
                } finally {

                }
            }
        }.start();
    }
    public void initConnect(URI uri){
        this.uri = uri;
        client = new JWebSocketClient(uri){
            @Override
            public void onOpen(ServerHandshake handshakedata) {
                super.onOpen(handshakedata);
                Log.e("JWebSocketClientServer","open Socket");
                sendToBoard("initOK",null);
            }

            @Override
            public void onClose(int code, String reason, boolean remote) {
                super.onClose(code, reason, remote);
                Log.e("JWebSocketClientServer","close Socket");
                sendToBoard("close",null);
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                Log.e("JWebSocketClientServer","Socket msg");
                sendToBoard("msg",message);
            }
            public void sendToBoard(String type,String msg){
                Intent intent = new Intent();
                intent.putExtra("type",type);
                intent.putExtra("message",msg);
                intent.setAction("com.example.agvctrl.beep");
                sendBroadcast(intent);
            }
            @Override
            public void onMessage(ByteBuffer bytes) {
                super.onMessage(bytes);
                Intent intent = new Intent();
                String msg;
                {
                    // 通过指定的字符集解码指定的byte数组并构造一个新的字符串
                     //msg = new String(bytes.array(), bytes.position(), bytes.limit(),"utf-8");
                    Charset charset = Charset.forName("utf-8");
                    CharBuffer charBuffer = charset.decode(bytes);
                    msg = charBuffer.toString();
                }

                intent.putExtra("message",msg);
                intent.setAction("com.example.agvctrl.beep");
                sendBroadcast(intent);
            }
        };
        connect();
    }
    public void sendMessage(String msg){
        if (null != client){
            client.send(msg);
        }
    }
    private void sendNotification(String msg){

    }
}
